package com.fivestyle.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fivestyle.common.result.R;
import com.fivestyle.entity.Book;
import com.fivestyle.entity.vo.BookVo;
import com.fivestyle.mapper.BookMapper;
import com.fivestyle.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.BoundKeyOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 张代钊
 * @since 2021-08-04
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {

    @Autowired
    private BookService bookService;


    @Override
    public R maxVisitBook(Long current, Long size) {

        Page<Book> bookPage = new Page<>(current,size);
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("visit").orderByDesc("created");
        IPage<Book> bookIPage = bookService.page(bookPage, wrapper);

        List<Book> records = bookIPage.getRecords();
        long total = bookIPage.getTotal();

        return R.ok().data("bookList",records).data("total",total);
    }

    @Transactional
    @Override
    public R addBook(Book book) {
        try {
            int insert = baseMapper.insert(book);
            if (insert>0){
                return R.ok().data("book",book);
            }else {
                return R.error().message("图书插入失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return R.error().message("图书插入报错");
        }
    }

    public Integer bookTotal(){
        return baseMapper.selectCount(new QueryWrapper<>());
    }

    @Override
    public R getBookListByType(String type) {
        List<Book> bookList = baseMapper.selectList(new QueryWrapper<Book>().eq("book_type", type));
        return R.ok().data("bookList",bookList);
    }

    @Override
    public R getBookListByName(String json) {
        JSONObject jsonObject = JSONObject.parseObject(json);

        String bookName = jsonObject.get("bookName").toString();
        String authorName = jsonObject.get("authorName").toString();

        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(bookName)){
            wrapper.like("book_name",bookName);
        }
        if (!StringUtils.isEmpty(authorName)){
            wrapper.like("author",authorName);
        }
        wrapper.orderByDesc("created");
        List<Book> bookList = baseMapper.selectList(wrapper);

        return R.ok().data("bookList",bookList);
    }

    @Override
    public ArrayList<HashMap<String,Object> > pie() {

        List<BookVo> books = baseMapper.groupByType();

        ArrayList<HashMap<String,Object> > list = new ArrayList<>();
        for (BookVo book : books) {
            HashMap<String, Object> mydata = new HashMap<>();
            mydata.put("name",book.getBookType());
            mydata.put("value", book.getCnt());
            list.add(mydata);
        }
        return list;
    }
}
